<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Model;
use Think\Model;
use User\Api\UserApi;

/**
 * 文档基础模型
 */
class MemberModel extends Model{

    /* 用户模型自动完成 */
    protected $_auto = array(
        array('login', 0, self::MODEL_INSERT),
        array('last_login_ip', 0, self::MODEL_INSERT),
        array('last_login_time', 0, self::MODEL_INSERT),
        array('update_time', NOW_TIME),
        array('status', 1, self::MODEL_INSERT),
    );

    /**
     * 登录指定用户
     * @param  integer $uid 用户ID
     * @return boolean      ture-登录成功，false-登录失败
     */
    // 添加appToken参数  liubo 2016-06-07
    public function login($uid, $appToken = null){
        /* 检测是否在当前应用注册 */
        $user = service('Users')->find($uid);
        if(!$user || $user['status'] < 1){ //未注册
            $this->error = '用户不存在';
            return false;
        }

        // 未设置昵称时，不记录用户登录session
        // by liubo at 2016-05-25
        if(empty($user['nickname'])) {
            $this->error = '昵称未设置';
            return $user;
        }

        /* 登录用户 */
        $this->autoLogin($user, $appToken);

        // 记录行为 2015-7-20 11:14:18 Legend. 取消
        // action_log('user_login', 'member', $uid, $uid);

        return $user;
    }

    /**
     * 注销当前用户
     * @return void
     */
    public function logout(){

        // session('home_user_auth', null);
        // session('home_user_auth_sign', null);

        // 退出登录销毁session  liubo 2016-7-15
        session('[destroy]');
        // add destory cookie by susy.liang<liangling@ilongyuan.com.cn>
        // 2015-7-20 11:06:11 Legend. 修改@
        cookie('auto_username',null, 'prefix=login');
        cookie('auto_login',null, 'prefix=login');
        return true;
    }

    /**
     * 自动登录用户
     * @param  integer $user 用户信息数组
     */
    // 添加appToken参数 liubo 2016-06-07
    public function autoLogin($user, $appToken = null){
        /* 更新登录信息 */
        service('Users')->updateLogin($user['id']);

        /* 记录登录SESSION和COOKIES */
        if(!empty($user['avatar'])){
            uri_format($user['avatar']);
        }
        if (empty($appToken)) {
            $tokenMdl = service('Token');
            $tokenMdl->addToken($user['id'], get_client_ip(), $appToken, 0);
        }
        $auth = array(
            'uid'             => $user['id'],
            'avatar'          => $user['avatar'],
            'username'        => $user['nickname'],
            'nickname'        => $user['nickname'],
            'last_login_time' => $user['last_login_time'],
            'token'           => $appToken
        );
        
        session('home_user_auth', $auth);
        session('home_user_auth_sign', data_auth_sign($auth));
        $data = [
            'uid'       => $user['id'],
            'nickname'  => $user['nickname'],
            'appToken'  => $appToken,
        ];
        session('app_auth', $data);
        session('app_auth_sign', data_auth_sign($data));
    }

}
